package rpc.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RetryConfig implements java.io.Serializable {

  private List<Integer> delayTimes;
  private int maxCount = 0;

  public RetryConfig() {
    this.delayTimes = new ArrayList<>();
    this.delayTimes.add(1);
  }

  public List<Integer> getDelayTimes() {
    return delayTimes;
  }

  public RetryConfig setDelayTimes(Integer... delayTimes) {
    return setDelayTimes(new ArrayList<>(Arrays.asList(delayTimes)));
  }


  public RetryConfig setDelayTimes(List<Integer> delayTimes) {
    this.delayTimes = delayTimes;
    return this;
  }

  public long nextKey() {
    Integer step = this.delayTimes.get(0);
//    System.out.println("step:" + step);
    long key = System.currentTimeMillis() / 1000 + step;

    if (this.delayTimes.size() > 1) {
      this.delayTimes.remove(0);
    }
    return key;
  }

  public boolean next() {
    if (maxCount == -2) {
      return true;
    }
    maxCount--;
    return maxCount >= 0;
  }


  public RetryConfig setMaxCount(int maxCount) {
    this.maxCount = maxCount;
    return this;
  }
}
